package org.sixtynine.stock.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.sixtynine.stock.Constants;
import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.BaseEntity;
import org.sixtynine.stock.entity.CustomerUpgrade;
import org.sixtynine.stock.entity.IpgReturn;
import org.sixtynine.stock.entity.PackageData;
import org.sixtynine.stock.entity.PaymentConfig;
import org.sixtynine.stock.entity.User;
import org.sixtynine.stock.model.LoadingSearchCriteria;
import org.sixtynine.stock.service.CustomerUpgradeService;
import org.sixtynine.stock.service.GenericService;
import org.sixtynine.stock.service.IpgReturnService;
import org.sixtynine.stock.service.PackageDataService;
import org.sixtynine.stock.service.UserService;
import org.sixtynine.stock.service.business.StockUtil;
import org.sixtynine.stock.service.content.DataManagerService;
import org.sixtynine.stock.service.file.FileDataService;
import org.sixtynine.stock.util.DateUtil;
import org.sixtynine.stock.util.SessionBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Thilan
 * 
 *         this class use to add , edit and delete ipg return data
 * 
 */

@Controller
@SessionAttributes
public class IpgReturnController extends BackEndAuthController {

	/** TODO configure */
	private int RECORDS_PER_PAGE = 10;

	@Autowired
	private GenericService genericService;

	@Autowired
	private FileDataService fileDataService;

	@Autowired
	DataManagerService dataManagerService;

	@Autowired
	IpgReturnService ipgReturnService;

	@Autowired
	CustomerUpgradeService customerUpgradeService;

	@Autowired
	PackageDataService packageDataService;

	@Autowired
	UserService userService;

	@RequestMapping(value = "/admin/ipg_return/search")
	public ModelAndView search(@ModelAttribute LoadingSearchCriteria criteria) {
		ModelAndView modelAndView = new ModelAndView("/admin/ipg_return/list");
		loadData(1, -1, criteria.getCode(), criteria.getFromDate(),
				criteria.getToDate(), modelAndView, criteria);

		return modelAndView;
	}

	@RequestMapping(value = "/admin/ipg/add/process")
	public ModelAndView addingIpgReturn(HttpServletRequest request,
			@ModelAttribute @Valid IpgReturn ipgReturn, BindingResult result) {

		ModelAndView modelAndView = null;
		/*
		 String orderId = ""; SessionBean sessionBean =
		 SessionBean.getInstance(request); User logedUser =
		 sessionBean.getUser();
		 
		 int customerReg = 2;
		 
		 if (customerReg == Constants.IPG_ACCEPTED) {
		 
		 CustomerUpgrade customerUpgrade = customerUpgradeService
		 .searchCustomerUpgrade(orderId);
		 
		 PackageData orderPackage = packageDataService
		 .findById(customerUpgrade.getPackg().getId());
		  
		 logedUser.setUserCategory(orderPackage.getUserCategory());
		 customerUpgrade.setIpgStatus(Constants.IPG_ACCEPTED);
		 
		 genericService.saveOrUpdate(ipgReturn);
		 genericService.saveOrUpdate(logedUser);
		 genericService.saveOrUpdate(customerUpgrade);
		 
		 modelAndView = new ModelAndView("/admin/ipg/list"); loadData(1, -1,
		 null, null, null, modelAndView, null);
		 modelAndView.addObject("userLogged", true); } else { modelAndView =
		 new ModelAndView("/admin/ipg_return/add");
		 modelAndView.addObject("ipgReturn", new IpgReturn());
		 modelAndView.addObject("userLogged", true); }
		 */
		return modelAndView;

	}

	@RequestMapping(value = "/admin/ipg_return/list")
	public ModelAndView listOfIpgReturn(
			HttpServletRequest request,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "totalRecords", required = false) Integer totalRecords) {

		ModelAndView modelAndView = null;

		if (!isUserLogged(request)) {
			modelAndView = new ModelAndView("/login/login");
			modelAndView.addObject("registedUser", new User());
			return modelAndView;
		}
		modelAndView = new ModelAndView("/admin/ipg_return/list");
		loadData(page, totalRecords, null, null, null, modelAndView, null);
		modelAndView.addObject("userLogged", true);

		return modelAndView;
	}

	/**
	 * load data
	 * 
	 * @param page
	 * @param totalRecords
	 * @param companyId
	 * @param fromDate
	 * @param toDate
	 * @param modelAndView
	 */
	private void loadData(Integer page, Integer totalRecords, String name,
			Date fromDate, Date toDate, ModelAndView modelAndView,
			LoadingSearchCriteria criteria) {
		GridPramDTO gridPramDTO = new GridPramDTO();
		gridPramDTO.setRows(RECORDS_PER_PAGE);
		if (page != null) {
			gridPramDTO.setPage(page);
		} else {
			gridPramDTO.setPage(1);
		}
		if (totalRecords != null) {
			gridPramDTO._setTotalNumberOfRecords(totalRecords);
		}

		Page<IpgReturn> ipgReturnPage = ipgReturnService.searchIpgReturn(
				fromDate, toDate, "date", gridPramDTO);
		modelAndView.addObject("ipgReturnPage", ipgReturnPage);

		if (criteria == null) {
			criteria = new LoadingSearchCriteria();
			criteria.setName(name);
		}
		modelAndView.addObject("criteria", criteria);

	}

	@RequestMapping(value = "/admin/ipg_return/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editIpgReturn(HttpServletRequest request,
			@PathVariable Integer id) {
		ModelAndView modelAndView = null;

		if (!isUserLogged(request)) {
			modelAndView = new ModelAndView("/login/login");
			modelAndView.addObject("registedUser", new User());
			return modelAndView;
		}
		modelAndView = new ModelAndView("/admin/ipg_return/edit");
		BaseEntity ipgReturn = genericService.findById(id, IpgReturn.class);
		modelAndView.addObject("ipgReturn", ipgReturn);
		modelAndView.addObject("userLogged", true);

		return modelAndView;
	}

	@RequestMapping(value = "/admin/ipg_return/edit/{id}", method = RequestMethod.POST)
	public ModelAndView edditingIpgReturn(
			@ModelAttribute @Valid IpgReturn ipgReturn, BindingResult result,
			@PathVariable Integer id) {

		if (!result.hasErrors()) {
			genericService.saveOrUpdate(ipgReturn);

			ModelAndView modelAndView = new ModelAndView(
					"/admin/ipg_return/list");

			List<BaseEntity> ipgReturns = genericService
					.findAll(IpgReturn.class);
			modelAndView.addObject("ipgReturns", ipgReturns);

			loadData(1, -1, ipgReturn.getOrderId(), ipgReturn.getAddedDate(),
					ipgReturn.getAddedDate(), modelAndView, null);

			String message = "Ipg Return was successfully edited.";
			modelAndView.addObject("message", message);

			return modelAndView;
		} else {
			ModelAndView modelAndView = new ModelAndView(
					"/admin/ipg_return/edit");
			return modelAndView;
		}
	}

	@RequestMapping(value = "/admin/ipg_return/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteIpgReturn(@PathVariable Integer id) {
		genericService.delete(id, IpgReturn.class);

		ModelAndView modelAndView = new ModelAndView("/admin/ipg_return/list");

		loadData(1, -1, null, null, null, modelAndView, null);

		String message = "Ipg Return was successfully deleted.";
		modelAndView.addObject("message", message);
		return modelAndView;
	}

}
